import { EnumMicroService } from "../types/types";
import * as process from "node:process";
import { resolve } from "path";
import * as dotenv from "dotenv";

function loadEnv() {
  const envFile =
    process.env.NODE_ENV === "development"
      ? ".env.development"
      : ".env.production";
  console.log(`加载的env：${envFile}`);
  const envConfig = dotenv.config({
    path: resolve(process.cwd(), envFile),
  }).parsed;
  const commonConfig = dotenv.config({
    path: resolve(process.cwd(), ".env"),
  }).parsed;

  if (envConfig.error) {
    console.error("envConfig load error", envConfig.error);
  }
  if (commonConfig.error) {
    console.error("commonConfig load error", commonConfig.error);
  }

  const finalConfig = { ...commonConfig, ...envConfig };
  for (const key in finalConfig) {
    process.env[key] = finalConfig[key];
  }
}

loadEnv();

// 定义配置接口
// interface Configuration {
//   token_expires_in: string;
//   open_auth: boolean;
//   gateway: {
//     http_port: number;
//     socket_io_port: number;
//     socket_io_namespace: string;
//     etcd_service_name: EnumMicroService;
//     nats_client_name: string;
//     pacs_proxy_path: string;
//   };
//   systemServer: {
//     system_name: EnumMicroService;
//     http_port: number;
//     socket_io_namespace: EnumMicroService;
//     create_dicom_folder_path: string;
//     etcd_service_name: EnumMicroService;
//     nats_client_name: string;
//     proxy_path: string;
//   };
//   rcServer: {
//     port: number;
//     path: string;
//     etcd_service_name: EnumMicroService;
//   };
//   dicomServer: {
//     system_name: EnumMicroService;
//     http_port: number;
//     socket_io_namespace: EnumMicroService;
//     etcd_service_name: EnumMicroService;
//     nats_client_name: string;
//     proxy_path: string;
//   };
//   imageServer: {
//     system_name: EnumMicroService;
//     http_port: number;
//     socket_io_namespace: EnumMicroService;
//     etcd_service_name: EnumMicroService;
//     nats_client_name: string;
//     proxy_path: string;
//   };
//   breathServer: {
//     system_name: EnumMicroService;
//     userVersion: number;
//     v1_ws_http_port: number;
//     http_port: number;
//     socket_io_namespace: EnumMicroService;
//     etcd_service_name: EnumMicroService;
//     nats_client_name: string;
//   };
//   pacsServer: {
//     url: string;
//     username: string;
//     password: string;
//   };
//   etcdServer: {
//     host: string;
//     gateAuth: {
//       username: string;
//       password: string;
//     };
//     lease: number;
//     service_prefix: string;
//   };
//   hardwareServer: {
//     system_name: EnumMicroService;
//     hardware_ip: string;
//     http_port: number;
//     hardware_port: number;
//     nats_client_name: string;
//   };
//   natsServer: string[];
//   logPathPrefix: string;
//   soft_version: string;
// }

const getLogPath = () => {
  console.log("解析log文件的位置");
  const logPrefix = process.env["LOG_PATH_PREFIX"] || "D:\\ct\\ct-log";
  const logPath = resolve(logPrefix);
  console.log(`解析得到的log文件的位置是：${logPath}`);
  return logPath;
};

const NATS_PREFIX = "NATS_";
export const configuration = {
  token_expires_in: "30d",
  open_auth: true,
  gateway: {
    http_port: parseInt(process.env["GATEWAY_HTTP_PORT"]),
    socket_io_port: parseInt(process.env["GATEWAY_SOCKET_IO_PORT"]),
    socket_io_namespace: process.env["GATEWAY_SOCKET_IO_NAMESPACE"],
    etcd_service_name: EnumMicroService.gateway,
    nats_client_name: `${NATS_PREFIX}${EnumMicroService.gateway}`,
    pacs_proxy_path: process.env["GATEWAY_PACS_PROXY_PATH"],
  },
  systemServer: {
    system_name: EnumMicroService.system,
    http_port: 9100,
    socket_io_namespace: EnumMicroService.system,
    create_dicom_folder_path: process.env["CREATE_DICOM_FOLDER_PATH"],
    etcd_service_name: EnumMicroService.system,
    nats_client_name: `${NATS_PREFIX}${EnumMicroService.system}`,
    proxy_path: `${EnumMicroService.system}`,
  },
  rcServer: {
    port: parseInt(process.env["RC_SERVER_HTTP_PORT"]),
    path: "/",
    etcd_service_name: EnumMicroService.remoteDesktop,
  },
  dicomServer: {
    system_name: EnumMicroService.dicom,
    http_port: 9200,
    socket_io_namespace: EnumMicroService.dicom,
    etcd_service_name: EnumMicroService.dicom,
    nats_client_name: `${NATS_PREFIX}${EnumMicroService.dicom}`,
    proxy_path: `${EnumMicroService.dicom}`,
  },
  imageServer: {
    system_name: EnumMicroService.image,
    http_port: 8080,
    socket_io_namespace: EnumMicroService.image,
    etcd_service_name: EnumMicroService.image,
    nats_client_name: `${NATS_PREFIX}${EnumMicroService.image}`,
    proxy_path: `${EnumMicroService.image}`,
  },
  breathServer: {
    system_name: EnumMicroService.breath,
    userVersion: 1, // 1是老版本 2是新版本
    v1_ws_http_port: 9094,

    http_port: 9300,
    socket_io_namespace: EnumMicroService.breath,
    etcd_service_name: EnumMicroService.breath,
    nats_client_name: `${NATS_PREFIX}${EnumMicroService.breath}`,
  },
  pacsServer: {
    url: `http://${process.env.PACS_SERVER}:${process.env["PACS_PORT"]}`,
    username: process.env.PACS_USERNAME,
    password: process.env.PACS_PASSWORD,
  },
  etcdServer: {
    host: `http://${process.env.ETCD_SERVER}:2379`,
    auth: {
      username: process.env.ETCD_USERNAME,
      password: process.env.ETCD_PASSWORD,
    },
    lease: 2,
    service_prefix: "service-",
  },
  hardwareServer: {
    system_name: EnumMicroService.hardware,
    hardware_ip: "127.0.0.1",
    http_port: 9400,
    hardware_port: 11111,
    nats_client_name: `${NATS_PREFIX}${EnumMicroService.hardware}`,
  },
  natsServer: [`nats://${process.env.NAT_SERVER}:4222`],
  logPathPrefix: getLogPath(),
  soft_version: "1.0.0",
};

export const RBAC = {
  roles: ["admin", "high", "low"],
  permissions: [
    {
      name: "get",
      desc: "get",
    },
    {
      name: "set",
      desc: "set",
    },
    {
      name: "delete",
      desc: "delete",
    },
  ],
  rolePermissionMap: {
    admin: ["get", "set", "delete"],
    high: ["get", "set"],
    low: ["get"],
  },
  user: [
    {
      username: "admin",
      password: "123",
      role: "admin",
    },
    {
      username: "high",
      password: "123",
      role: "high",
    },
    {
      username: "low",
      password: "123",
      role: "low",
    },
  ],
};

export const PACS_SETTING = {
  instanceUrl: `${configuration.pacsServer.url}/instances`,
  studyUrl: `${configuration.pacsServer.url}/studies`,
  seriesUrl: `${configuration.pacsServer.url}/series`,
};

// 权限相关 需要对应权限的接口。 需要和数据库中保持一致。
export const NeedAuthPath = [
  "/system/get",
  "/system/set",
  "/system/delete"
]

export const ProxyServers = [
  configuration.systemServer,
  configuration.dicomServer,
  configuration.imageServer,
];
